﻿using Swsk33.GISEndExercise.Model;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Swsk33.GISEndExercise.Util
{
	/// <summary>
	/// 用于在画布上画图的实用类
	/// </summary>
	public class ElementPainter
	{
		/// <summary>
		/// 绘制线段的笔实例
		/// </summary>
		private Pen linePen = new Pen(Color.Blue, 2f);

		/// <summary>
		/// 着重选择绘制笔
		/// </summary>
		private Pen strongPen = new Pen(Color.Orchid, 3f);

		/// <summary>
		/// 绘制外包矩形的笔实例
		/// </summary>
		private Pen envelopPen = new Pen(Color.Red, 1.8f);

		/// <summary>
		/// 绘制点的笔刷实例
		/// </summary>
		private Brush pointBrush = new SolidBrush(Color.Red);

		/// <summary>
		/// 要绘制的画布
		/// </summary>
		private Graphics graphics;

		/// <summary>
		/// 构造器依赖注入
		/// </summary>
		/// <param name="graphics">传入画布对象</param>
		public ElementPainter(Graphics graphics)
		{
			this.graphics = graphics;
			this.graphics.CompositingQuality = CompositingQuality.HighQuality;
			this.graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			this.graphics.SmoothingMode = SmoothingMode.HighQuality;
		}

		/// <summary>
		/// 在当前画布上画一个点
		/// </summary>
		/// <param name="point">传入点对象</param>
		public void drawPoint(Point point)
		{
			// 点的直径
			int sideLength = 6;
			// 绘制点中心
			Point center = new Point(point.X - sideLength / 2, point.Y - sideLength / 2);
			Rectangle rectangle = new Rectangle(center, new Size(sideLength, sideLength));
			// 绘制圆点
			graphics.FillEllipse(pointBrush, rectangle);
		}

		/// <summary>
		/// 在当前画布上画一条线
		/// </summary>
		/// <param name="line">线段</param>
		public void drawLine(Line line)
		{
			graphics.DrawLine(linePen, line.Start, line.End);
		}

		/// <summary>
		/// 在当前画布上画一个多边形
		/// </summary>
		/// <param name="polygon">传入多边形</param>
		public void DrawPolygon(Polygon polygon)
		{
			graphics.DrawPolygon(linePen, polygon.Points);
		}

		/// <summary>
		/// 着重绘制多边形
		/// </summary>
		/// <param name="polygon">传入多边形</param>
		public void DrawPolygonStrong(Polygon polygon)
		{
			graphics.DrawPolygon(strongPen, polygon.Points);
		}

		/// <summary>
		/// 传入一个多边形绘制外包矩形
		/// </summary>
		/// <param name="polygon">传入多边形</param>
		public void DrawEnvelope(Polygon polygon)
		{
			float xMin = polygon.Points[0].X;
			float xMax = xMin;
			float yMin = polygon.Points[0].Y;
			float yMax = yMin;
			for (int i = 1; i < polygon.Points.Length; i++)
			{
				if (polygon.Points[i].X < xMin)
				{
					xMin = polygon.Points[i].X;
				}
				else if (polygon.Points[i].X > xMax)
				{
					xMax = polygon.Points[i].X;
				}
				if (polygon.Points[i].Y < yMin)
				{
					yMin = polygon.Points[i].Y;
				}
				else if (polygon.Points[i].Y > yMax)
				{
					yMax = polygon.Points[i].Y;
				}
			}
			graphics.DrawRectangle(envelopPen, xMin, yMin, xMax - xMin, yMax - yMin);
		}
	}
}